package com.guigu.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.dto.StorageGatherDto;
import com.guigu.erp.pojo.StorageCell;
import com.guigu.erp.pojo.StorageGather;
import com.guigu.erp.pojo.StorageGatherDetails;
import com.guigu.erp.service.StorageCellService;
import com.guigu.erp.service.StorageGatherDetailsService;
import com.guigu.erp.service.StorageGatherService;
import com.guigu.erp.utils.NumberUtils;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.StorageGatherTwoVo;
import com.guigu.erp.vo.StorageGatherVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

/**
 * ClassName:StorageGatherController
 * Package:com.guigu.erp.controller
 * Description:
 *
 * @Date:2024/3/19 17:18
 * @Author:chenchong
 */
@Controller
@RequestMapping("/StorageGather")
public class StorageGatherController {
    @Autowired
    private NumberUtils numberUtils;
    @Autowired
    private StorageGatherService StorageGatherService;
    @Autowired
    private StorageGatherDetailsService storageGatherDetailsService;
    @Autowired
    private StorageCellService storageCellService;

    @RequestMapping("/add")
    @ResponseBody
    public String add(@RequestBody StorageGatherDto dto) {
        //生成物料设计单编号
        String typeNumber = numberUtils.getTypeNumber("401");
        dto.setGatherId(typeNumber);
        dto.setCheckTag("S001-0");
        dto.setStoreTag("K002-1");
        BigDecimal num = new BigDecimal(0);
        dto.setGatheredAmountSum(num);
        StorageGatherService.page1(dto);
        return "ok";
    }

    @RequestMapping("/ShowAudit")
    @ResponseBody
    public PageUtils<StorageGather> updateAudit() {

        PageUtils<StorageGather> Utils = StorageGatherService.updateAudit();
        return Utils;
    }

    @RequestMapping("/showById")
    @ResponseBody
    public StorageGather showById(Integer id) {

        StorageGather byId = StorageGatherService.getById(id);
        return byId;
    }

    @RequestMapping("/StorageGatherDetailsById")
    @ResponseBody
    public PageUtils<StorageGatherDetails> StorageGatherDetailsById(Integer id) {
        QueryWrapper<StorageGatherDetails> qw = new QueryWrapper<>();
        qw.eq("`PARENT_ID", id);
        List<StorageGatherDetails> list = storageGatherDetailsService.list(qw);
        PageInfo<StorageGatherDetails> p = new PageInfo<>(list);
        PageUtils<StorageGatherDetails> utils = new PageUtils<>();
        utils.setTotal(p.getTotal());
        utils.setRows(p.getList());
        return utils;

    }

    @RequestMapping("/updateById")
    @ResponseBody
    public Boolean updateById(@RequestBody StorageGather storageGather) {
        boolean b = StorageGatherService.updateById(storageGather);
        return b;
    }

    @RequestMapping("/show")
    @ResponseBody
    public PageUtils<StorageGather> show(StorageGatherVo vo, int pageNo, int pageSize) {
        PageUtils<StorageGather> show = StorageGatherService.show(vo, pageNo, pageSize);
        return show;
    }

    @RequestMapping("/twoShow")
    @ResponseBody
    public PageUtils<StorageGather> twoShow(StorageGatherTwoVo vo, int pageNo, int pageSize) {
        PageUtils<StorageGather> show = StorageGatherService.twoShow(vo, pageNo, pageSize);
        return show;
    }

    @RequestMapping("/getoneById")
    @ResponseBody
    public StorageGatherDetails getoneById(int id) {
        StorageGatherDetails byId = storageGatherDetailsService.getById(id);
        return byId;
    }

    @RequestMapping("/getId")
    @ResponseBody
    public StorageGather getId(int id) {
        QueryWrapper<StorageGather> qw = new QueryWrapper<>();
        qw.eq("id", id);
        StorageGather byId = StorageGatherService.getOne(qw);
        return byId;
    }

    @RequestMapping("/updateTWO")
    @ResponseBody
    public boolean updateTWO(int id, int gatheredAmount) {
        StorageGatherDetails s = new StorageGatherDetails();
        s.setId(id);
        BigDecimal b = new BigDecimal(gatheredAmount);
        s.setGatheredAmount(b);
        s.setGatherTag("K002-2");
        boolean b1 = storageGatherDetailsService.updateById(s);
        return b1;
    }

    @Transactional
    @RequestMapping("/updateTWOById")
    @ResponseBody
    public boolean updateTWOById(int id,String reasonexact) {
        BigDecimal b = new BigDecimal(0);
        QueryWrapper<StorageGatherDetails> qw = new QueryWrapper<>();
        qw.eq("PARENT_ID", id);
        //入库明细
        List<StorageGatherDetails> list = storageGatherDetailsService.list(qw);
        for (int i = 0; i < list.size(); i++) {
            QueryWrapper<StorageCell> qw1 = new QueryWrapper<>();
            qw1.eq("PRODUCT_ID", list.get(i).getProductId());
            //入库数量
            List<StorageCell> list1 = storageCellService.list(qw1);
            for (int j = 0; j < list1.size(); j++) {
                if (list.get(i).getProductId().equals(list1.get(j).getProductId())) {
                    BigDecimal amount = list1.get(j).getAmount();
                    BigDecimal b1 = amount.add(list.get(i).getGatheredAmount());
                    list1.get(j).setAmount(b1);
                    storageCellService.updateById(list1.get(j));
                }
            }
            BigDecimal gatheredAmount = list.get(i).getGatheredAmount();
            b = b.add(gatheredAmount);
        }
        StorageGather storageGather = new StorageGather();
        storageGather.setId(id);
        storageGather.setGatheredAmountSum(b);
        storageGather.setStoreTag("K002-2");
        storageGather.setReasonexact(reasonexact);
        boolean b1 = StorageGatherService.updateById(storageGather);
        return b1;
    }
}
